/**   
* @Title: Application.java
* @Package com.an.nopro.app
* @Description: 项目启动类。
* @author antch,861710865@qq.com   
* @date 2017年8月6日 下午8:49:22
* @version V1.0   
*/

package com.an.nopro.app;

import java.util.concurrent.Executor;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonAutoDetect;

/**
 * @ClassName: Application
 * @Description: Spring boot Mybatis启动类
 * @author antch,861710865@qq.com
 * @date 2017年8月6日 下午8:49:22
 * 
 */
//@EnableTask
@EnableCaching//开启基于注解的缓存
@EnableScheduling // 开启定时任务支持
@SpringBootApplication(scanBasePackages = { "com.an.nopro.app" })
// @MapperScan("com.an.nopro.mapper.spring")
public class Application {

	public static void main(String[] args) {

		SpringApplication.run(Application.class, args);
	}
	/**
     * 自定义定时任务线程池
     *  如果没有，则使用默认定时任务池
     * @return
     */
    @Bean(destroyMethod="shutdown")
    public Executor taskExecutor() {
        return  new ScheduledThreadPoolExecutor(10);
    }

	/**
	 * @param redisConnectionFactory
	 * @return 目的：redisTemplate .<br>
	 * 
	 */
	@Bean
	public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
		RedisTemplate<Object, Object> template = new RedisTemplate<>();
		template.setConnectionFactory(redisConnectionFactory);
		Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
		ObjectMapper objectMapper = new ObjectMapper();
		objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		jackson2JsonRedisSerializer.setObjectMapper(objectMapper);

		// 设置值的序列化采用JACKSON
		template.setValueSerializer(jackson2JsonRedisSerializer);
		// shezhi shezhikey采用StringRedisSerializer
		template.setKeySerializer(new StringRedisSerializer());

		template.afterPropertiesSet();
		return template;
	}

}
